//
//  ListView.swift
//  DianXia
//
//  Created by you&me on 2017/12/20.
//  Copyright © 2017年 you&me. All rights reserved.
//

import UIKit
fileprivate let cellIder = "listCell"

class ListView: UIView {

    var listDataSource: [String]? {
        didSet {
            addSubview(listTabView)
        }
    }
    let selectRow = Variable("")

    lazy var listTabView: UITableView = {
        let tab = UITableView(frame: self.bounds, style: .plain)
        tab.tableFooterView = UIView()
        tab.rowHeight = 30
        tab.delegate = self
        tab.dataSource = self
        return tab
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 3
        self.layer.masksToBounds = true
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.lightGray.cgColor
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension ListView: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listDataSource?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: cellIder)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: cellIder)
        }
        cell?.textLabel?.text = listDataSource?[indexPath.row]
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        tableView.deselectRow(at: indexPath, animated: true)
        selectRow.value = listDataSource?[indexPath.row] ?? ""
    }

}
